<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item> <i class="el-icon-setting"></i> 咨询订单 </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <!-- <el-button type="primary" class="handle-del mr10" @click="addClick">新增</el-button> -->
        <el-date-picker v-model="query.time" class="mr10" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        value-format="yyyy-MM-dd">
        </el-date-picker>
        <el-select v-model="query.guwenid" filterable placeholder="选择跟进人" class="mr10">
          <el-option label="全部" value=""></el-option>
          <el-option v-for="item in guwenList" :label="item.aName" :value="item.aId"></el-option>
        </el-select>
        <!-- <el-select v-model="query.fromType" filterable placeholder="选择转换状态" class="mr10">
          <el-option label="全部" value=""></el-option>
          <el-option label="已转换" value=""></el-option>
          <el-option label="未转换" value=""></el-option>
        </el-select> -->
        <el-select v-model="query.status" filterable placeholder="选择订单状态" class="mr10">
          <el-option label="全部" value=""></el-option>
          <el-option label="咨询跟进" value="0"></el-option>
          <el-option label="已签约" value="1"></el-option>
          <el-option label="已回访" value="2"></el-option>
        </el-select>
      </div>
      <div class="handle-box">
        <el-input placeholder="请输入姓名查询" class="handle-input mr10" v-model="query.kw"></el-input>
        <el-button class="mr10" type="primary" icon="el-icon-search" @click="souClick">搜索</el-button>
      </div>
      <el-table :data="tableData" border class="table" style="width: 100%" ref="multipleTable" header-cell-class-name="table-header">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column type="index" :index="tableIndex" label="序号" width="55" align="center"></el-table-column>
        <el-table-column prop="userName" width="120" label="姓名"></el-table-column>
        <el-table-column prop="userPhone" width="120" label="电话"></el-table-column>
        <el-table-column prop="applyQuota" width="120" label="申请金额"></el-table-column>
        <el-table-column prop="actualQuota" width="120" label="实际批款金额"></el-table-column>
        <el-table-column prop="cost" width="120" label="实际批款点位"></el-table-column>
        <el-table-column prop="loanType" width="120" label="贷款方式"></el-table-column>
        <el-table-column prop="cityName" width="120" label="办理城市"></el-table-column>
        <el-table-column prop="createTime" width="120" label="创建时间"></el-table-column>
        <el-table-column fixed="right" label="跟进人" width="120" align="center">
          <template slot-scope="scope">
            <el-tag>{{scope.row.adviserName}}</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column fixed="right" label="转化状态" width="150" align="center">
          <template slot-scope="scope">
            <el-tag>未转换</el-tag>
            <el-tag type="success">已转换</el-tag>
          </template>
        </el-table-column> -->
        <el-table-column fixed="right" label="订单状态" width="120" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == '0'">咨询跟进</el-tag>
            <el-tag v-if="scope.row.status == '1'" type="success">已签约完成</el-tag>
            <el-tag v-if="scope.row.status == '2'" type="success">已回访</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" @click="editClick(scope.row)">编辑</el-button>
            <!-- <el-button type="text" icon="el-icon-edit" @click="editClick">转化</el-button> -->
            <el-button type="text" icon="el-icon-edit" @click="signingClick(scope.row)">签约完成</el-button>
            <el-button type="text" icon="el-icon-edit" @click="questionnaireClick">回访问卷</el-button>
            <el-button type="text" icon="el-icon-setting" class="red" @click="delClick(scope.row.oId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background layout="total, prev, pager, next" :current-page="query.page" :page-size="query.limit" :total="query.totalCount"
                       :pager-count="5" @current-change="PageChange" @prev-click="PageChange" @next-click="PageChange"></el-pagination>
      </div>
    </div>
    <!-- 编辑 -->
    <el-dialog title="详细信息" :visible.sync="user.flg" width="35%">
      <el-form ref="form" :model="user.form" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="user.form.name" readonly></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="user.form.tel" readonly></el-input>
        </el-form-item>
        <el-form-item label="申请金额">
          <el-input v-model="user.form.money"></el-input>
        </el-form-item>
        <el-form-item label="贷款方式">
          <el-select v-model="user.form.type" placeholder="请选择" style="width:100%;">
            <el-option v-for="item in daiList" :label="item.ptName" :value="item.ptId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="办理城市">
          <el-cascader v-model="user.form.city" :options="cityArr" style="width:100%;"></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="user.flg = false">取 消</el-button>
        <el-button type="primary" @click="editsub" :loading="user.load">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 签约信息 -->
    <el-dialog title="签约信息" :visible.sync="signing.flg" width="35%">
      <el-form ref="form" :model="signing.form" label-width="100px">
        <el-form-item label="实际批款金额">
          <el-input v-model="signing.form.shijinum"></el-input>
        </el-form-item>
        <el-form-item label="实际批款点位">
          <el-input v-model="signing.form.dian"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="signing.flg = false">取 消</el-button>
        <el-button type="primary" @click="qianClick" :loading="signing.load">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 问卷回访 -->
    <el-dialog title="问卷回访" :visible.sync="questionnaire.flg" width="35%">
      <p style="text-align:center;padding-bottom:15px;font-weight:bold;font-size:16px;">将二维码发送给 张三</p>
      <div class="qrCodeUrl" id="qrCodeUrl" ref="qrCodeUrl"></div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="questionnaire.flg = false">取 消</el-button>
        <el-button type="primary" @click="questionnaire.flg = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { webDadviserInit, webDorderInit, webDorderdaiInit, webDorderEditSub, webDorderQianSub, webDorderDel } from '../../../api/index'
import QRCode from 'qrcodejs2';
import CITYARR from '../../../utils/city';
export default {
  data() {
    return {
      CITYARR: CITYARR,
      cityArr: [],
      query: {
        page: 1,
        limit: 10,
        kw: '',
        time: '',
        guwenid: '',
        status: '',
        totalCount: 0,
      },
      guwenList: [],
      daiList: [],
      tableData: [],
      //编辑
      user: {
        editid: '',
        flg: false,
        load: false,
        form: {
          name: '',
          tel: '',
          money: '',
          type: '',
          city: [],
          cityname: '',
          citycode: '',
        }
      },
      //签约
      signing: {
        flg: false,
        load: false,
        qianid: '',
        form: {
          shijinum: '',
          dian: ''
        }
      },
      //问卷
      questionnaire: {
        flg: false,
      }
    }
  },
  created() {
    this.CITYARR.forEach((item, i) => {
      this.cityArr.push({
        value: item.value + '-' + item.code,
        label: item.label,
        children: []
      })
      item.children.forEach(e => {
        this.cityArr[i].children.push({
          value: e.value + '-' + e.code,
          label: e.label,
        })
      })
    });
    this.guwenInit();
    this.getInit();
    this.daiInit();
  },
  methods: {
    //初始化顾问
    guwenInit() {
      webDadviserInit({
        page: 1,
        limit: 1000000,
        kw: ''
      }).then(res => {
        if (res.code == 0) {
          this.guwenList = res.recordList;
        } else {
          this.$message(res.msg);
        }
      })
    },
    //初始化贷款分类
    daiInit() {
      webDorderdaiInit().then(res => {
        if (res.code == 0) {
          this.daiList = res.typeList;
        } else {
          this.$message(res.msg);
        }
      })
    },
    //初始化列表
    getInit() {
      webDorderInit({
        page: this.query.page,
        limit: this.query.limit,
        startTime: this.query.time ? this.query.time[0] : '',
        endTime: this.query.time ? this.query.time[1] : '',
        kw: this.query.kw,
        adviserId: this.query.guwenid,
        status: this.query.status
      }).then(res => {
        if (res.code == 0) {
          this.tableData = res.recordList;
          this.query.totalCount = res.totalCount;
        } else {
          this.$message(res.msg);
        }
      })
    },
    //序号
    tableIndex(index) {
      return (this.query.page - 1) * this.query.limit + index + 1;
    },
    //搜索
    souClick() {
      this.query.page = 1;
      this.getInit();
    },
    //翻页
    PageChange(num) {
      this.query.page = num;
      this.getInit();
    },
    //编辑
    editClick(item) {
      this.user.flg = true;
      this.user.editid = item.oId;
      this.user.form.name = item.userName;
      this.user.form.tel = item.userPhone;
      this.user.form.money = item.applyQuota;
      this.user.form.type = item.productTypeId;
      this.user.form.cityname = item.cityName;
      this.user.form.citycode = item.cityCode;
      if (item.cityName) {
        this.user.form.city = (() => {
          var arr = [];
          this.cityArr.forEach(e => {
            if (item.cityName.split('/')[0] == e.label) {
              arr[0] = e.value;
            }
          })
          arr[1] = item.cityName.split('/')[1] + '-' + item.cityCode;
          return arr;
        })();
      }
    },
    //编辑提交
    editsub() {
      this.user.load = true;
      webDorderEditSub({
        oId: this.user.editid,
        applyQuota: this.user.form.money,
        productTypeId: this.user.form.type,
        loanType: (() => {
          var str = '';
          this.daiList.forEach(item => {
            if (item.ptId == this.user.form.type) {
              str = item.ptName;
            }
          })
          return str;
        })(),
        cityName: this.user.form.city[0].split("-")[0] + "/" + this.user.form.city[1].split("-")[0],
        cityCode: this.user.form.city[1].split("-")[1]
      }).then(res => {
        this.user.load = false;
        if (res.code == 0) {
          this.user.flg = false;
          this.$message.success("编辑成功");
          this.getInit();
        } else {
          this.$message(res.msg);
        }
      })
    },
    //签约
    signingClick(item) {
      this.signing.flg = true;
      this.signing.qianid = item.oId;
      this.signing.form.shijinum = item.actualQuota;
      this.signing.form.dian = item.cost;
    },
    //签约提交
    qianClick() {
      this.signing.load = true;
      webDorderQianSub({
        oId: this.signing.qianid,
        actualQuota: this.signing.form.shijinum,
        cost: this.signing.form.dian
      }).then(res => {
        this.signing.load = false;
        if (res.code == 0) {
          this.signing.flg = false;
          this.$message.success('提交成功');
          this.getInit();
        } else {
          this.$message(res.msg);
        }
      })
    },
    //删除
    delClick(id) {
      webDorderDel({
        oid: id
      }).then(res => {
        if (res.code == 0) {
          this.$message.success('删除成功');
          this.getInit();
        } else {
          this.$message(res.msg);
        }
      })
    },
    //问卷
    questionnaireClick() {
      this.questionnaire.flg = true;
      setTimeout(() => {
        document.getElementById('qrCodeUrl').innerHTML = '';
        this.creatQrCode("66666666666666", document.getElementById('qrCodeUrl'));
      }, 0)
    },
    //生成二维码
    creatQrCode(text, item) {
      new QRCode(item, {
        text: text,
        width: 200,
        height: 200,
        colorDark: '#000000',
        colorLight: '#ffffff',
        correctLevel: QRCode.CorrectLevel.H
      })
    },
  }
}
</script>
<style scoped>
.handle-input {
    width: 300px;
    display: inline-block;
}
.qrCodeUrl >>> img {
    margin: 0 auto;
}
</style>